package com.wiscamp.ninechapters.accounts.adapter.resources;

import com.wiscamp.ninechapters.accounts.application.dto.command.DepositCommand;
import com.wiscamp.ninechapters.accounts.application.dto.command.WithdrawCommand;
import com.wiscamp.ninechapters.accounts.application.dto.vo.AccountLogVO;
import com.wiscamp.ninechapters.accounts.application.dto.vo.AccountVO;
import com.wiscamp.ninechapters.accounts.application.servies.AccountAppService;
import com.wiscamp.ninechapters.common.rest.ResourceHelper;
import com.wiscamp.ninechapters.common.rest.RestResponse;
import cube.ddd.aspect.AopResourceException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/")
@Tag(name = "账户服务-积分信息")
public class AccountResource {
    @Autowired
    private AccountAppService accountAppService;

    @Autowired
    public AccountResource(AccountAppService accountAppService) {
        this.accountAppService = accountAppService;
    }

    @GetMapping("users/{userId}/accounts/{accountType}")
    @Operation(summary = "通过用户编号获取账户积分")
    @AopResourceException
    public RestResponse<AccountVO> getAccount(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("accountType") int accountType) {
        var vo = accountAppService.getAccountByType(accountType, userId);
        return ResourceHelper.getOkResponse(request, response, vo);
    }

    @GetMapping("users/{userId}/accounts/{accountType}/logs")
    @Operation(summary = "通过用户编号获取用户积分日志")
    @AopResourceException
    public RestResponse<Collection<AccountLogVO>> getAccountLogs(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathVariable("userId") long userId, @PathVariable("accountType") int accountType) {
        var logs = accountAppService.getAccountLogs(accountType, userId);
        return ResourceHelper.getOkResponse(request, response, logs);
    }

    @PutMapping("users/{userId}/accounts/deposit")
    @Operation(summary = "增加用户账户积分")
    @AopResourceException
    public RestResponse<Boolean> deposit(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DepositCommand command) {
        boolean result = accountAppService.deposit(command);
        return ResourceHelper.getOkResponse(request, response, result);
    }

    @PutMapping("users/{userId}/accounts/withdraw")
    @Operation(summary = "减少用户账户积分")
    @AopResourceException
    public RestResponse<Boolean> withdraw(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody WithdrawCommand command) {
        boolean result = accountAppService.withdraw(command);
        return ResourceHelper.getOkResponse(request, response, result);
    }
}
